//
//  HSYNewShopViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/17.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYNewShopViewController.h"
#import "HSYSelectProvinceViewController.h"
#import "HSYNewShopRequest.h"
#import "HSYNewShopModel.h"
#import "NSString+Validation.h"
#import "BQActivityView.h"

@interface HSYNewShopViewController ()

@property (weak, nonatomic) IBOutlet UITextField *shortNameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *cellphoneTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *cityTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *addressTextFiled;

@property (nonatomic, strong) HSYNewShopModel *shopModel;

@end

@implementation HSYNewShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.titleButton setTitle:@"新增店铺" forState:UIControlStateNormal];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)comitBtnClick:(id)sender{
    if (![self checkAvalibale]) return;
    
    HSYNewShopRequest *task = [[HSYNewShopRequest alloc] init];
    task.cellphone = self.shopModel.cellphone;
    task.address = self.shopModel.address;
    task.shortName = self.shopModel.shortName;
    task.districtCode = self.shopModel.districtCode;
    task.uid = [HSYUserCenter instatce].appAuUser.uid;
    
    [BQActivityView showActiviTy];
    __weak typeof(self) wSelf = self;
    
    [HSYNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        [BQActivityView hideActiviTy];
        
        if (wSelf.completeBlock) {
            if (responseObject) {
                wSelf.completeBlock();
                [wSelf handleGoBackEvent:nil];
            }else if (resultString){
                [wSelf showMessage:resultString];
            }
        }
    }];
}

- (BOOL)checkAvalibale{
    [self.view endEditing:YES];
    
    self.shopModel.shortName = self.shortNameTextFiled.text;
    self.shopModel.cellphone = self.cellphoneTextFiled.text;
    self.shopModel.address = self.addressTextFiled.text;
    
    if (self.shopModel.shortName.length == 0) {
        [self showMessage:@"请填写店铺简称"];
        return NO;
    }
    
    //手机号选填
//    if (self.shopModel.cellphone.length == 0) {
//        [self showMessage:@"请填写联系人手机号"];
//        return NO;
//    }
    
    if (self.shopModel.cellphone.length > 0 && (![self.shopModel.cellphone isTelephone]) ) {
        [self showMessage:@"请填写正确的座机/手机号"];
        return NO;
    }

    if (!self.shopModel.districtCode) {
        [self showMessage:@"请选择商铺所在地"];
        return NO;
    }
    
    if (self.shopModel.address.length == 0) {
        [self showMessage:@"请填写详细地址"];
        return NO;
    }
    
    return YES;
}

- (IBAction)selectCityBtnClick:(id)sender{
    [self.view endEditing:YES];
    
    HSYSelectProvinceViewController *vc = [[HSYSelectProvinceViewController alloc] init];
    
    __weak typeof(self)weakSelf = self;
    [vc getProvinceCityCountyinfo:^(NSString *title, NSString *code) {
        weakSelf.cityTextFiled.text = title;
        weakSelf.shopModel.districtCode = code;
    }];
    
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - 懒加载shopmodel -
- (HSYNewShopModel *)shopModel{
    if (!_shopModel) {
        _shopModel = [[HSYNewShopModel alloc] init];
    }
    return _shopModel;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
